#region license
/*
 * Copyright (C) 2007 CchenSoft.com
 * 
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License 2.1 or later, as
 * published by the Free Software Foundation. See the included License.txt
 * or http://www.gnu.org/copyleft/lesser.html for details.
 * 
 * Author: Billy Zhang
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace CchenSoft.Portal.Util
{
    public class ConvertUtil
    {
        public static int ToInt32(string value)
        {
            if (string.IsNullOrEmpty(value))
                return 0;

            return Convert.ToInt32(value);
        }

        public static string ToString(object obj)
        {
            if (obj == null)
                return string.Empty;

            return obj.ToString();
        }

        public static string ToBase64String(string value)
        {
            return Convert.ToBase64String(Encoding.Default.GetBytes(value));
        }

        public static string FromBase64String(string value)
        {
            return Encoding.Default.GetString(Convert.FromBase64String(value));
        }

        public static DateTime ToDateTime(string value)
        {
            return Convert.ToDateTime(value);
        }

        public static DateTime? ToDateTime2(string value)
        {
            if (string.IsNullOrEmpty(value))
                return null;

            try
            {
                return Convert.ToDateTime(value);
            }
            catch
            {
            }

            return null;
        }

        public static decimal ToDecimal(object obj)
        {
            string str = ToString(obj);

            try
            {
                return Convert.ToDecimal(str);
            }
            catch
            {
            }

            return 0;
        }
    }
}
